package serialization;

import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.io.*;

/**
 *
 * @author sdecrem
 */

public class ActionPublic {
    public static void writeGraphe(String filename, GraphePublic toSave) throws Exception{
        XMLEncoder encoder =
           new XMLEncoder(
              new BufferedOutputStream(
                new FileOutputStream(filename)));
        encoder.writeObject(toSave);
        encoder.close();
    }

    public static void readGraphe(String filename) throws Exception {
        XMLDecoder decoder =
            new XMLDecoder(
                new BufferedInputStream(
                    new FileInputStream(filename)));
        //GraphePublic.setInstance((GraphePublic)decoder.readObject());
        decoder.readObject();
        decoder.close();
    }

    public static void main (String [] args) throws Exception{
        GraphePublic origine = new GraphePublic() ;
        //on considere qu'on a affaire a un singleton grace a getInstance()
        origine.getInstance().setChemin(0, "chaine1") ;
        origine.getInstance().setChemin(1, "chaine2") ;
        origine.getInstance().setChemin(2, "chaine3") ;
        origine.getInstance().setChemin(3, "chaine4") ;
        origine.getInstance().setVille(0, "chaine5") ;
        origine.getInstance().setVille(1, "chaine6") ;
        origine.getInstance().setVille(2, "chaine7") ;
        origine.getInstance().setVille(3, "chaine8") ;
        origine.getInstance().setLac(0, "chaine9") ;
        origine.getInstance().setRiviere(0, "chaine10") ;

        System.out.println(origine.getInstance()) ;
        ActionPublic.writeGraphe("/tmp/graphe1.xml", origine);
        System.out.println(origine.getInstance()) ;

        /*Action.readGraphe("/tmp/graphe1.xml") ;
        System.out.println(GraphePublic.getInstance()) ;*/
    }
}